package com.uzai.console.common.utils;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @Auther: jianghan
 * @Date: 2021/9/17 11:13
 * @Description:
 */
public class MsgSplitUtil {

    private static Logger logger = LoggerFactory.getLogger(MsgSplitUtil.class);
    private static final char[] SPECIAL_CHARS = new char[] {'|','[',']'};

    /**
     * 获取延迟时间
     * @param regex
     * @return
     */
    public static int getDelayTime(String regex){
        try {
            Pattern pattern = compile("\\d{1,5}");
            Matcher matcher = pattern.matcher(regex);
            while (matcher.find()) {
                return Integer.parseInt(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return 0;
    }

    /**
     * 获取随机字符长度
     * @param regex
     * @return
     */
    public static int getRandomStrLen(String regex){
        try {
            Pattern pattern = compile("\\d{1,5}");
            Matcher matcher = pattern.matcher(regex);
            while (matcher.find()) {
                return Integer.parseInt(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return 0;
    }

    /**
     * 获取分段符
     * @param content
     * @return
     */
    public static Set<String> getSplitRegEx(String content){
        Set<String> list = new LinkedHashSet<>();
        try {
            Pattern pattern = compile("\\[分段符\\|\\d{1,5}\\]|\\[分段符\\]|\\[IMG\\]|\\[VIDEO\\]|\\[/IMG\\]|\\[/VIDEO\\]|\\[IMG\\|\\d{1,5}\\]|\\[VIDEO\\|\\d{1,5}\\]");
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                list.add(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return list;
    }

    /**
     * 获取随机字符变量
     * @param content
     * @return
     */
    public static List<String> getRandomStrArgs(String content){
        List<String> list = new ArrayList<>();
        try {
            Pattern pattern = compile("\\[随机字符\\|\\d{1,5}\\]");
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                list.add(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return list;
    }

    /**
     * 获取超级指令变量
     * @param content
     * @return
     */
    public static Set<String> getSuperCmdArgs(String content){
        Set<String> list = new LinkedHashSet<>();
        try {
            Pattern pattern = compile("\\[超级指令\\|[\\s\\S]*?\\]");
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                list.add(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return list;
    }

    /**
     * 获取红包卡片变量
     * @param content
     * @return
     */
    public static Set<String> getRedpackArgs(String content){
        Set<String> list = new LinkedHashSet<>();
        try {
            Pattern pattern = compile("\\[红包卡片\\|[\\s\\S]*?\\]");
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                list.add(matcher.group(0));
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return list;
    }

    /**
     * 替换随机字符
     * @param content
     * @return
     */
    public static String replaceRandomStrArgs(String content){
        List<String> args = getRandomStrArgs(content);
        for (String arg:args){
            int length = getRandomStrLen(arg);
            String randomStr = "";
            if (length>0){
                randomStr = RandomStringUtils.random(length,true,true);
            }
            //content = content.replace(arg,randomStr);
            content = content.replaceFirst("\\[随机字符\\|\\d{1,5}\\]",randomStr);
        }
        return content;
    }

    /**
     * 拆分消息
     * @param contents
     * @param splitRegex
     * @return
     */
    public static List<String> split(List<String> contents,String splitRegex){
        List<String> results = new ArrayList<>();
        for (String content:contents){
            String[] childStrs = content.split(escapeRegex(splitRegex));
            results.addAll(Arrays.asList(childStrs));
        }
        return results;
    }


    /**
     * 转义
     * @param regex
     * @return
     */
    public static String escapeRegex(String regex) {
        if(StringUtils.isBlank(regex)) {
            return regex;
        }

        StringBuffer sb = new StringBuffer();
        char[] args = regex.toCharArray();
        for(char c1 : args) {
            for(char c2 : SPECIAL_CHARS) {
                if(c1 == c2) {
                    sb.append("\\");
                    break;
                }
            }
            sb.append(c1);
        }

        return sb.toString();
    }

    /**
     * 处理超级指令
     * @param content
     * @param cmds
     * @return
     */
    private static String replaceSuperCmdArgs(String content, Set<String> cmds) {
        Set<String> superCmdArgs = getSuperCmdArgs(content);
        for (String superCmdArg:superCmdArgs){
            content = content.replace(superCmdArg,"");
            String superCmd = getSuperCmd(superCmdArg);
            if (StringUtils.isNotBlank(superCmd)){
                cmds.add(superCmd);
            }
        }
        return content;
    }
    /**
     * 处理红包卡片
     * @param content
     * @param cmds
     * @return
     */
    private static String replaceRedpackCmdArgs(String content, Set<String> cmds) {
        Set<String> redpackCmdArgs = getRedpackArgs(content);
        for (String redpackCmdArg:redpackCmdArgs){
            content = content.replace(redpackCmdArg,"");
            String redpackCmd = getRedpackCmd(redpackCmdArg);
            if (StringUtils.isNotBlank(redpackCmd)){
                cmds.add(redpackCmd);
            }
        }
        return content;
    }


    /**
     * 获取超级指令
     * @param superCmdArg
     * @return
     */
    private static String getSuperCmd(String superCmdArg) {
        try {
            Pattern pattern = compile("(?<=\\[超级指令\\|)([\\s\\S]*?)(?=\\])");
            Matcher matcher = pattern.matcher(superCmdArg);
            while (matcher.find()) {
                return matcher.group(0);
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return null;
    }

    /**
     * 获取红包卡片
     * @param superCmdArg
     * @return
     */
    private static String getRedpackCmd(String superCmdArg) {
        try {
            Pattern pattern = compile("(?<=\\[红包卡片\\|)([\\s\\S]*?)(?=\\])");
            Matcher matcher = pattern.matcher(superCmdArg);
            while (matcher.find()) {
                return matcher.group(0);
            }
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(replaceRandomStrArgs("中国人[随机字符|6]雄起来[随机字符|6]"));
    }
}
